package com.pybox.api;

import com.pybox.service.TSessionService;
import com.pybox.dto.TSessionDto;

import com.pybox.dto.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;

/**
* 描述：TSession Api
* @author wuyingkai
* @date 2018-09-15
*/

@Slf4j
@RequestMapping("/TSession")
@Api(tags = "TSession管理接口", description = "TSession管理接口")
@RestController
@ApiIgnore
public class TSessionApi {

    @Autowired
    private TSessionService service;

    @ApiOperation(value = "创建TSession", notes = "创建TSession")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result<Boolean> save(@RequestBody TSessionDto dto) {
        service.create(dto);
        return Result.ok(true);
    }

    @ApiOperation(value = "删除TSession", notes = "删除TSession")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParam(name = "id", value = "TSession id", dataType = "String", paramType = "path")
    public Result<Boolean> delete(@PathVariable("id") Integer id) {
        service.delete(id);
        return Result.ok(true);
    }

    @ApiOperation(value = "更新TSession", notes = "更新TSession")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    @ApiImplicitParam(name = "id", value = "TSession id", dataType = "String", paramType = "path")
    public Result<Boolean> update(@PathVariable("id") Integer id, @RequestBody TSessionDto dto) {
        service.update(id, dto);
        return Result.ok(true);
    }

    @ApiOperation(value = "查询TSession", notes = "查询TSession")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "TSession id", dataType = "String", paramType = "path")
    public Result<TSessionDto> detail(@PathVariable("id") Integer id) {

        return Result.ok(service.detail(id));
    }

    @ApiOperation(value = "分页查询TSession", notes = "分页查询TSession")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ApiImplicitParams({
           @ApiImplicitParam(name = "sessionId", value = "sessionId", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "loginName", value = "loginName", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "deptName", value = "deptName", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "ipaddr", value = "ipaddr", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "loginLocation", value = "loginLocation", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "browser", value = "browser", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "os", value = "os", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "status", value = "status", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "startTimestamp", value = "startTimestamp", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "lastAccessTime", value = "lastAccessTime", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "expireTime", value = "expireTime", dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "pageNo", value = "pageNo", dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "pageSize", dataType = "int", paramType = "query")
    })
    public Result<Page<TSessionDto>> page(
                        @RequestParam(required = false) String sessionId,
                        @RequestParam(required = false) String loginName,
                        @RequestParam(required = false) String deptName,
                        @RequestParam(required = false) String ipaddr,
                        @RequestParam(required = false) String loginLocation,
                        @RequestParam(required = false) String browser,
                        @RequestParam(required = false) String os,
                        @RequestParam(required = false) String status,
                        @RequestParam(required = false) String startTimestamp,
                        @RequestParam(required = false) String lastAccessTime,
                        @RequestParam(required = false) String expireTime,
                    @RequestParam(required = true) Integer pageNo,
                    @RequestParam(required = true) Integer pageSize) {

        Map<String, String> params = new HashMap<>();
                params.put("sessionId",sessionId);
                params.put("loginName",loginName);
                params.put("deptName",deptName);
                params.put("ipaddr",ipaddr);
                params.put("loginLocation",loginLocation);
                params.put("browser",browser);
                params.put("os",os);
                params.put("status",status);
                params.put("startTimestamp",startTimestamp);
                params.put("lastAccessTime",lastAccessTime);
                params.put("expireTime",expireTime);
        return Result.ok(service.page(pageNo, pageSize, params));
    }
}